package com.apptonelabs.android.controller;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.apptonelabs.android.model.PNRResponse;
import com.apptonelabs.android.model.PNRStatus;
import com.google.gson.Gson;

public class PNRStatusCrawller {
	public static void main(String args[]) {
		try {
			String pnr1 = "6327174335";
			PNRStatusCrawller check = new PNRStatusCrawller();
			String data = check.getPNRResponse("http://127.0.0.1:8888/pnr?pnr=" + pnr1);
			if(data != null) {
				Gson gson = new Gson();
				PNRResponse res = gson.fromJson(data, PNRResponse.class);
				PNRStatus pnr = res.getStatus();
				if(pnr != null) {
					pnr.setPnr(pnr1);
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getPNRResponse(String uri) throws Exception {
		HttpClient Client = new DefaultHttpClient();
		String json = null;

		// Create Request to server and get response

		HttpGet httpget = new HttpGet(uri);
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		json = Client.execute(httpget, responseHandler);
		
		return json;
	}
}
